package com.apexsoft.front.common.utils;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apexsoft.front.common.support.Result;

/**
 * service层数据返回对象json封装
 * @author dinglei
 *
 */
public class JsonResult {
	
	private JSONObject result = null;
	
	public JsonResult(){
		result = new JSONObject();
		result.put("success", false);
		result.put(Result.NOTE, "");
	}
	
	public JsonResult(boolean success){
		this();
		result.put("success", success);
	}
	
	public JsonResult(boolean success,String note){
		this();
		result.put("success", success);
		result.put(Result.NOTE, note);
	}
	
	public JsonResult putRecords(JSONArray array){
		result.put(Result.TOTAL, array==null?0:array.size());
		result.put(Result.RECORDS, array);
		return this;
	}
	
	public JsonResult putObject(JSONObject obj){
		result.put("obj", obj);
		return this;
	}
	
	public JsonResult putString(String key,String value){
		result.put(key, value);
		return this;
	}
	public JsonResult put(String key,Object value){
		result.put(key, value);
		return this;
	}
	public JsonResult setSuccess(boolean success){
		result.put("success", success);
		return this;
	}
	
	public JsonResult setNote(String note){
		result.put(Result.NOTE, note);
		return this;
	}
	
	/**
	 * 返回结果json对象
	 * @return
	 */
	public JSONObject getResult(){
		//LogUtil.infoLog(result.toString());
		return result;
	}
	
	public JSONObject getExceptionResult(Exception e){
		e.printStackTrace();
		result.put("success", false);
		result.put(Result.NOTE, "系统异常："+e.getMessage());
		return result;
	}
	public JsonResult putRecords(String name,JSONArray array){
		result.put(name, array);
		return this;
	}
	
	public JsonResult putRecords(String name,List<?> list){
		result.put(name, list);
		return this;
	}

}
